package com.webboard
import com.authen.*;

class ReplyController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    def authenticateService  //Use for Check About User Login
    
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [replyInstanceList: Reply.list(params), replyInstanceTotal: Reply.count()]
    }

    def create = {
        def replyInstance = new Reply()
        replyInstance.properties = params
        return [replyInstance: replyInstance]
    }

    def save = {
        def replyInstance = new Reply(params)
        def postInstance = Post.get(params["postId"].toInteger())
        postInstance.count_reply += 1 // Counter Reply in My Post
        replyInstance.id_replyOfPost = postInstance.count_reply //set replyID of post

        replyInstance.replyDate = new Date() //set ,when User Reply this
        replyInstance.ipAddress_client = request.getRemoteAddr()//set ,User Ip Address

        if (replyInstance.save(flush: true)) {
            postInstance.addToReplies(replyInstance)
            if(authenticateService.isLoggedIn()){ // if Login User Add Who are user Reply 
                def userPosted = User.get(loggedInUserInfo(field:'id').toInteger())
                userPosted.addToReplies(replyInstance)
            }
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'reply.label', default: 'Reply'), replyInstance.id])}"
            //redirect(action: "show", id: replyInstance.id)
            redirect(action: "show", id: postInstance.id, controller:"post")
        }
        else {
            //render(view: "create", model: [replyInstance: replyInstance])
            System.out.println("eRR InRrPly");
            redirect(action: "show" ,controller:"post",id:params["postId"].toInteger(),params:["replyparams":params])
        }
    }

    def show = {
        def replyInstance = Reply.get(params.id)
        
        if (!replyInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'reply.label', default: 'Reply'), params.id])}"
            redirect(action: "list")
        }
        else {
            [replyInstance: replyInstance]
        }
    }

    def edit = {
        def replyInstance = Reply.get(params.id)
        if (!replyInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'reply.label', default: 'Reply'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [replyInstance: replyInstance]
        }
    }

    def update = {
        def replyInstance = Reply.get(params.id)
        if (replyInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (replyInstance.version > version) {
                    
                    replyInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'reply.label', default: 'Reply')] as Object[], "Another user has updated this Reply while you were editing")
                    render(view: "edit", model: [replyInstance: replyInstance])
                    return
                }
            }
            replyInstance.properties = params
            if (!replyInstance.hasErrors() && replyInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'reply.label', default: 'Reply'), replyInstance.id])}"
                redirect(action: "show", id: replyInstance.id)
            }
            else {
                render(view: "edit", model: [replyInstance: replyInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'reply.label', default: 'Reply'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def replyInstance = Reply.get(params.id)
        if (replyInstance) {
            try {
                replyInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'reply.label', default: 'Reply'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'reply.label', default: 'Reply'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'reply.label', default: 'Reply'), params.id])}"
            redirect(action: "list")
        }
    }
}
